#include "stdafx.h"
#include "mousecontroller.hpp"
#include <boost/bind.hpp>
#include "actor_manager.hpp"
#include <pantheios/inserters/real.hpp>
#include <pantheios/inserters/integer.hpp>
namespace FndGame2D {
  mouse_controller::mouse_controller(const std::string& name ):
    quad_actor(name){

    updater2.connect(sigc::mem_fun(this, &mouse_controller::send_mouse_posinfo)) ;
  }    
  void mouse_controller::send_mouse_posinfo(float dummy) {
    thinker_manager* m_mgr = get_property<thinker_manager*>(thinker_manager::HAS_MANAGER, (thinker_manager*)0) ;
    if(!m_mgr) return ;
    sf::RenderWindow& r = m_mgr->system_window() ;
    sf::Vector2f mouse_pos = r.ConvertCoords(
					     r.GetInput().GetMouseX(),
					     r.GetInput().GetMouseY()) ;
    unsigned int width = r.GetWidth() ;
    unsigned int height = r.GetHeight() ;
    int left = static_cast<int>(width * 0.1f) ;
    int right = static_cast<int>(width * 0.9f) ;
    int top = static_cast<int>(height * 0.9f) ;
    int bottom = static_cast<int>(height * 0.1f) ;
    float pos_x = x() ;
    float pos_y = y() ;
    if(mouse_pos.x <= left) {
      pos_x -= .1f ; ;
    } else if(mouse_pos.x >= right) {
      pos_x += 0.1f ;
    }
    if(mouse_pos.y >= top) {
      pos_y += .1f ;
    } else if(mouse_pos.y <= bottom) pos_y -= 0.1f ;

    if(m_mgr->current_render_view()) {
      m_mgr->current_render_view()->Move(x() - pos_x, y() - pos_y) ;
    }
    set_position(mouse_pos) ;
  }
}
